package com.example.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.sharker_demo.R;
import com.example.models.data.Study.StudyRecyclerviewData;
import com.example.utils.FrescoImageLoader;
import com.facebook.drawee.view.SimpleDraweeView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;

/**
 * 董建业  时间:2017/4/11.
 */

public class StudyRecyclerviewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<StudyRecyclerviewData> data;
    private ItemOnclick ItemOnclick;
    private boolean isstudy;

    public void setItemOnclick(com.example.ui.adapter.ItemOnclick itemOnclick) {
        ItemOnclick = itemOnclick;
    }

    public StudyRecyclerviewAdapter(Context context, ArrayList<StudyRecyclerviewData> data) {
        this.context = context;
        this.data = data;
        this.isstudy = true;
    }

    public StudyRecyclerviewAdapter(Context context, ArrayList<StudyRecyclerviewData> data,boolean isstudy) {
        this.context = context;
        this.data = data;
        this.isstudy = isstudy;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new HeadVHolder(LayoutInflater.from(context).inflate(R.layout.item_study_haid, parent, false));
        } else if (viewType == 1) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_study_recyclerview, parent, false));
        } else if (viewType == 4) {
            return new BannerVHolder(LayoutInflater.from(context).inflate(R.layout.item_study_banner, parent, false));
        } else {
            return new GridVHolder(LayoutInflater.from(context).inflate(R.layout.item_study_grid, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (!isstudy)
            return 1;

        if (position == 0) {
            return 4;
        } else if (position == 1 || position == 5 || position == 9) {
            return 0;
        } else if (position < 10) {
            return 1;
        } else {
            return 2;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            HeadVHolder holder2 = (HeadVHolder) holder;
            holder2.titleleft.setText(data.get(position).head);

            holder2.titleright.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ItemOnclick.OnClick(position,"");
                }
            });
            if (position != 1)
                holder2.topspacing.setVisibility(View.VISIBLE);
        } else if (itemViewType == 1) {

            StudyRecyclerviewAdapter.ViewHolder holder1 = (ViewHolder) holder;
            holder1.image.setImageResource(R.drawable.teacher);
            holder1.title.setText(data.get(position).title);
            holder1.title2.setText(data.get(position).title2);
            holder1.speaker.setText(data.get(position).speaker);
            holder1.length.setText(data.get(position).length + "分钟");

            int type = data.get(position).type;
            if (type == 1)
                holder1.type.setText("视频");
            else if (type == 2)
                holder1.type.setText("音频");
            else
                holder1.type.setText("免费");

            holder1.study_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ItemOnclick.OnClick(position, data.get(position).Object_id);
                }
            });

        } else if (itemViewType==2){
            GridVHolder holder3 = (GridVHolder) holder;
            holder3.img.setImageResource(R.drawable.home_page_content_picture_03_3x);
            holder3.titler.setText(data.get(position).title);
            holder3.titler2.setText(data.get(position).title2);

            holder3.study_item2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ItemOnclick.OnClick(position,data.get(position).Object_id);
                }
            });
        }else {
            ArrayList<String> listimage = new ArrayList<>();
            ArrayList<String> listtitle = new ArrayList<>();
            if (listimage.size() <= 1) {
                for (int i = 0; i < 5; i++) {
                    listimage.add(i, "http://i04.pic.sogou.com/83241acbca0c35e6");
                    listtitle.add(i, "标题: http://i04.pic.sogou.com/83241acbca0c35e6");
                }
            }
            ((BannerVHolder) holder).banner
                    .setDelayTime(1500)
                    .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
                    .setImageLoader(new FrescoImageLoader())
                    .setBannerTitles(listtitle)
                    .setImages(listimage)
                    .isAutoPlay(true)
                    .start();
        }



    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class BannerVHolder extends RecyclerView.ViewHolder {
        Banner banner;

        public BannerVHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.study_banner);
        }
    }

    class HeadVHolder extends RecyclerView.ViewHolder {
        TextView titleleft;
        TextView titleright;
        LinearLayout topspacing;

        public HeadVHolder(View itemView) {
            super(itemView);
            topspacing = (LinearLayout) itemView.findViewById(R.id.topspacing);
            titleleft = (TextView) itemView.findViewById(R.id.haid_lrft);
            titleright = (TextView) itemView.findViewById(R.id.haid_right);
        }
    }

    class GridVHolder extends RecyclerView.ViewHolder {
        TextView titler;
        TextView titler2;
        SimpleDraweeView img;
        LinearLayout study_item2;
        public GridVHolder(View itemView) {
            super(itemView);
            study_item2 = (LinearLayout) itemView.findViewById(R.id.study_item2);
            img = (SimpleDraweeView) itemView.findViewById(R.id.study_item_grid_img);
            titler = (TextView) itemView.findViewById(R.id.study_item_grid_title);
            titler2 = (TextView) itemView.findViewById(R.id.study_item_grid_title2);
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView image;
        LinearLayout study_item;
        TextView title;
        TextView title2;
        TextView speaker;
        TextView type;
        TextView length;

        public ViewHolder(View itemView) {
            super(itemView);
            study_item = (LinearLayout) itemView.findViewById(R.id.study_item);
            image = (SimpleDraweeView) itemView.findViewById(R.id.study_item_image_simpledrawwiew);
            title = (TextView) itemView.findViewById(R.id.study_item_title_tv);
            title2 = (TextView) itemView.findViewById(R.id.study_item_title2_tv);
            speaker = (TextView) itemView.findViewById(R.id.study_item_speaker_tv);
            type = (TextView) itemView.findViewById(R.id.study_item_type_tv);
            length = (TextView) itemView.findViewById(R.id.study_item_length_tv);
        }
    }

}
